layui.use(['table', 'admin', 'ax', 'func','treeTable'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var treeTable = layui.treeTable;
    var insTb;
    var langs = layui.data("system").lang;

    /**
     * 品类树表管理
     */
    var Category = {
        tableId: "categoryTable"
    };

    /**
     * 初始化表格的列
     */
    Category.initColumn = function () {
        return [
            {field: 'categoryName', sort: true, title: langs.FIELD_CATEGORY_NAME},
            {field: 'categoryImg', sort: true, title: 'Logo',width: 80,templet: function (d) {
                    if (typeof (d.categoryImg) == "undefined"||d.categoryImg == null ||d.categoryImg == ""){
                        return '';
                    } else{
                        return '<img src="'+d.categoryImg+'" style="width:30px;height:30px"/>';
                    }
                }},
            {field: 'remake', sort: true,width: 200, title: langs.FIELD_REMAKE},
            {align: 'center', toolbar: '#tableBar',width: 200, title: langs.FIELD_OPERATION}
        ];
    };


    /**
     * 跳转到添加页面
     */
    Category.jumpAddPage = function (data) {
        func.open({
            title: '添加类别',
            content: [Feng.ctxPath + '/category/add?id=' + data.id],
            height:650,
            tableId: Category.tableId,
            endCallback: function () {
                Category.initTable(Category.tableId);
            }
        });
    };


    /**
     * 跳转到编辑页面
     *
     * @param data 点击按钮时候的行数据
     */
    Category.jumpEditPage = function (data) {
        func.open({
            title: '编辑类别',
            content: Feng.ctxPath + '/category/edit?id=' + data.id +'&parentId='+data.parentId,
            height:650,
            tableId: Category.tableId,
            endCallback: function () {
                Category.initTable(Category.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Category.exportExcel = function () {
        var checkRows = table.checkStatus(Category.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Category.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/category/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Category.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };



    Category.initTable = function (menuId, reqData) {
        return treeTable.render({
            elem: '#' + Category.tableId,
            tree: {
                iconIndex: 0,    // 折叠图标显示在第几列
                isPidData: true,  // 是否是pid形式数据
                pidName: 'parentId'     // pid字段名称
            },
            height: "full-158",
            cellMinWidth: 100,
            cols: Category.initColumn(),

            reqData: function (data, callback) {
                var ajax = new $ax(Feng.ctxPath
                    + '/category/list', function (res) {
                    callback(res.data);
                    func.hoverOpenImg();


                }, function (res) {
                    Feng.error("加载失败!" + res.responseJSON.message + "!");
                });
                ajax.setData(reqData);
                ajax.start();
            }

        });
    };


    insTb = Category.initTable(Category.tableId);

    $('#expandAll').click(function () {
        insTb.expandAll();
    });
    $('#foldAll').click(function () {
        insTb.foldAll();
    });


    // 回车搜索
    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            var categoryName = $('#categoryName').val();
            Category.search(categoryName);
        }
    });
    // 搜索
    $('#btnSearch').click(function () {
        var categoryName = $('#categoryName').val();
        Category.search($.trim(categoryName));
    });


    Category.search =   function search(categoryName){
        if (categoryName) {
            insTb.filterLocalData(categoryName);
        } else {
            insTb.clearFilter();
        }
    }


    // 清除搜索
    $('#btnClearSearch').click(function () {
        insTb.clearFilter();
        $("#categoryName").val("");
    });


    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        Category.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        Category.exportExcel();
    });

    // 工具条点击事件
    treeTable.on('tool(' + Category.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'add') {
            Category.jumpAddPage(data);
        } else if (layEvent === 'edit') {
            Category.jumpEditPage(data);
        } else {
            Category.onDeleteItem(data);
        }
    });
});
